package a05_双指针法;

/**
 * <p>
 * a08_环形链表2复习4
 * </p>
 *
 * @author flyduck
 * @since 2025/2/20
 */
public class a08_环形链表2复习4 {
    public ListNode detectCycle(ListNode head) {
        ListNode slowNode = head;
        ListNode fastNode = head;

        while (fastNode != null && fastNode.next != null){
            slowNode = slowNode.next;
            fastNode = fastNode.next.next;
            if(fastNode == slowNode){
                ListNode node1 = head;
                ListNode node2 = slowNode;
                while (node1 != node2){
                    node1 = node1.next;
                    node2 = node2.next;
                }
                return node1;
            }
        }
        return null;
    }
}
